package com.tjec.project.system.controller;

import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.page.TableDataInfo;
import com.tjec.project.system.domain.BmsMdWorkflowConfig;
import com.tjec.project.system.service.IBmsMdWorkflowConfigService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程配置Controller
 *
 * @author liuxu
 * @date 2020-05-11
 */
@RestController
@RequestMapping("/system/workflowConfig")
public class BmsMdWorkflowConfigController extends BaseController {
    @Autowired
    private IBmsMdWorkflowConfigService bmsMdWorkflowConfigService;

    /**
     * 查询流程配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:workflowConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmsMdWorkflowConfig bmsMdWorkflowConfig) {
        startPage();
        List<BmsMdWorkflowConfig> list = bmsMdWorkflowConfigService.selectBmsMdWorkflowConfigList(bmsMdWorkflowConfig);
        return getDataTable(list);
    }

    /**
     * 导出流程配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:workflowConfig:export')")
    @Log(title = "流程配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsMdWorkflowConfig bmsMdWorkflowConfig) {
        List<BmsMdWorkflowConfig> list = bmsMdWorkflowConfigService.selectBmsMdWorkflowConfigList(bmsMdWorkflowConfig);
        ExcelUtil<BmsMdWorkflowConfig> util = new ExcelUtil<BmsMdWorkflowConfig>(BmsMdWorkflowConfig.class);
        return util.exportExcel(list, "workflowConfig");
    }

    /**
     * 获取流程配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:workflowConfig:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid) {
        return AjaxResult.success(bmsMdWorkflowConfigService.selectBmsMdWorkflowConfigByGuid(guid));
    }

    /**
     * 新增流程配置
     */
    @PreAuthorize("@ss.hasPermi('system:workflowConfig:add')")
    @Log(title = "流程配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsMdWorkflowConfig bmsMdWorkflowConfig) {
        return toAjax(bmsMdWorkflowConfigService.insertBmsMdWorkflowConfig(bmsMdWorkflowConfig));
    }

    /**
     * 修改流程配置
     */
    @PreAuthorize("@ss.hasPermi('system:workflowConfig:edit')")
    @Log(title = "流程配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsMdWorkflowConfig bmsMdWorkflowConfig) {
        return toAjax(bmsMdWorkflowConfigService.updateBmsMdWorkflowConfig(bmsMdWorkflowConfig));
    }

    /**
     * 删除流程配置
     */
    @PreAuthorize("@ss.hasPermi('system:workflowConfig:remove')")
    @Log(title = "流程配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(bmsMdWorkflowConfigService.deleteBmsMdWorkflowConfigByGuids(guids));
    }

    /**
     * 查询流程名称
     */
    @GetMapping("/getWorkflowName")
    public AjaxResult getWorkflowName(BmsMdWorkflowConfig bmsMdWorkflowConfig) {
        List<BmsMdWorkflowConfig> list = bmsMdWorkflowConfigService.selectBmsMdWorkflowConfigList(bmsMdWorkflowConfig);
        if (CollectionUtils.isNotEmpty(list)) {
            list.stream().forEach(item -> {
                item.setWorkflowName(item.getWorkflowName() + "-" + item.getWorkflowCode());
            });
        }
        return AjaxResult.success(list);
    }
    
    /**
     * 查询流程配置列表
     */
    @GetMapping("/workflowTypelist")
    public AjaxResult workflowTypelist() {
    	BmsMdWorkflowConfig bmsMdWorkflowConfig = new BmsMdWorkflowConfig();
        List<BmsMdWorkflowConfig> list = bmsMdWorkflowConfigService.selectBmsMdWorkflowConfigList(bmsMdWorkflowConfig);
        return AjaxResult.success(list);
    }
}
